﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UofCASE.AgileProductLinerDSL.Nant.Tasks.visualizing;

namespace UofCASE.AgileProductLinerDSL.Nant.Tasks.Models
{
    public class Platform : IEnumerable<ITest>
    {
        public ProductLinerSet owner { get; internal set; }
        IDictionary<Feature, TestSet> tests;
        public String Name { get; private set; }
        
        public bool Passed { 
            get {
                foreach (TestSet testSet in tests.Values)
                {
                    foreach (ITest test in testSet)
                    {
                        if (!test.Passed)
                            return false;
                    }
                }
                return true;
            }
        }
        
        public Platform(String name)
        {
            this.Name = name;
            tests = new Dictionary<Feature, TestSet>();
        }

        public void AddToFeature(Feature f, ITest t)
        {
            if (!tests.ContainsKey(f))
            {
                new TestSet(this, f);
            }
            TestSet set;
            tests.TryGetValue(f, out set);
            set.AddTest(t);
        }


        internal void setFor(TestSet testSet, Feature f)
        {
            TestSet set = TestsFor(f);
            if(set == null){
                tests.Add(f, testSet);
            }
        }


        internal TestSet TestsFor(Feature f)
        {
            TestSet set;
            tests.TryGetValue(f, out set);
            return set;
        }

        public IEnumerator<ITest> GetEnumerator()
        {
            return new CompositeEnumerator<ITest>(tests.Values.GetEnumerator() as IEnumerator<IEnumerable<ITest>>);
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        internal object Find(ITestKey key)
        {
            foreach(ITest test in this)
            {
                if (key.Matches(test))
                {
                    return test;
                }
            }
            return null;
        }

        public string Directory { 
            get 
            {
                return Name;
            }
        }

        internal string PlatformDir()
        {
            return owner.ProductLinerDir + "/Platforms/" + Name;
        }

        public System.IO.FileInfo CollectionPlan
        {
            get
            {
                return owner.CollectionPlan;
            }
        }

        public string AssemblyPath { 
            get 
            {
                return owner.AssemblyPath;
            } 
        }

        public string ToXml()
        {
            XmlElement platform = new XmlElement("Platform", "name", Name);
            foreach(Feature feature in tests.Keys)
            {
                XmlElement featureTag = new XmlElement("Feature", "name", feature.Name);
                foreach (Test test in tests[feature])
                {
                    XmlElement testTag = new XmlElement("Test", "name", test.TestFile.Name, "passed", test.Passed.ToString());
                    featureTag.AddChildElement(testTag);
                }
                platform.AddChildElement(featureTag);
            }
            return platform.render();
        }
    }
}
